package operation;

import abbot.finder.BasicFinder;
import abbot.finder.ComponentNotFoundException;
import abbot.finder.Matcher;
import abbot.finder.MultipleComponentsFoundException;
import abbot.finder.matchers.ClassMatcher;
import abbot.finder.matchers.NameMatcher;
import abbot.tester.JTabbedPaneLocation;
import abbot.tester.JTabbedPaneTester;
import abbot.tester.JTextComponentTester;
import com.supermap.desktop.controls.colorScheme.ColorScheme;
import com.supermap.desktop.controls.colorScheme.ColorsComboBox;
import com.supermap.desktop.controls.ui.controls.Dockbar;
import com.supermap.desktop.controls.ui.controls.DockbarManager;
import com.supermap.desktop.controls.ui.controls.TextFieldSearch;
import com.supermap.desktop.core.Application;
import com.supermap.desktop.core.Interface.IDockbarManager;
import util.Info;

import javax.swing.*;
import java.awt.*;

public class DockbarOperation extends Operation {
    Dockbar dockBar = null;

    public DockbarOperation(String title) {
        container = (Container) getDockbarByTitle(title).getInnerComponent();
    }
    public DockbarOperation() {
    }
    /**
     * 根据dockbar名称获取指定dockbar
     *
     * @param title
     * @return dockbar
     */
    public Dockbar getDockbarByTitle(String title) {
        IDockbarManager dockbarManager = Application.getActiveApplication()
                .getMainFrame().getDockbarManager();
        for (int i = 0; i < dockbarManager.getCount(); i++) {
            dockBar = (Dockbar) Application.getActiveApplication()
                    .getMainFrame().getDockbarManager().get(i);
            if (dockBar.getTitle().equals(title)) {
                return dockBar;
            }
        }
        return dockBar;
    }

    public static void setVisible(String title) {
        new DockbarOperation().getDockbarByTitle(title).setVisible(true);
    }
    /**
     * 点击dockbar中的选项卡,包括地图属性、属性、专题图
     *
     * @param tabName tab选项卡名称
     */
    public void clickTab(String tabName) {
        JTabbedPane pane = null;
        JTabbedPaneTester jtester = new JTabbedPaneTester();
        try {
            pane = (JTabbedPane) new BasicFinder().find(container, new ClassMatcher(JTabbedPane.class, true));
        } catch (ComponentNotFoundException | MultipleComponentsFoundException e) {
            //一般在docbarcontainer中只有一个tabbedpane，特殊如布局地图属性看起来不止一个
            if(e instanceof MultipleComponentsFoundException){
                Component[] panes = ((MultipleComponentsFoundException) e).getComponents();
                for (Component component : panes) {
                    int tabCount = ((JTabbedPane) component).getTabCount();
                    for (int i = 0; i < tabCount; i++) {
                        String tabTitle = ((JTabbedPane) component).getTitleAt(i);
                        if(tabTitle != null && tabTitle.equals(tabName)){
                            pane = (JTabbedPane) component;
                            break;
                        }
                    }
                    if(pane != null){
                        break;
                    }
                }
            }
//			e.printStackTrace();
        }
        //上面的方法可能更通用一些，如果上面方法遇到问题可以在看看是否需要用下面这种。目前（2018.3.6）的用例下面这种方法也是可用的。
        /*Component[] components = container.getComponents();
        for (int i = 0; i < components.length; i++) {
            if (components[i] instanceof JTabbedPane) {
                pane = (JTabbedPane) components[i];
            }
            if (components[i] instanceof JScrollPane) {
                pane = (JTabbedPane) ((JPanel) ((JScrollPane) components[i]).getViewport().getView()).getComponents()[0];
            }
            if (components[i] instanceof JPanel) {
                pane = (JTabbedPane) ((JPanel) components[i]).getComponents()[0];
            }
        }*/
        jtester.actionSelectTab(pane, new JTabbedPaneLocation(tabName));
        abbot.tester.Robot.delay(1000);
    }
    /**
     * 激活隐藏docbar，如地图属性切换到图层属性
     * @param tabName
     */
    public static void switchDockbar(String tabName) throws ComponentNotFoundException, MultipleComponentsFoundException {
        DockbarManager dockbarManager = (DockbarManager) Application.getActiveApplication()
                .getMainFrame().getDockbarManager();
        Component dockPort = dockbarManager.getDockPort();//docbarPort是出ribbon之外的下面所有框
        JTabbedPane pane = null;
        try {
            pane = (JTabbedPane) new BasicFinder().find((Container) dockPort,new ClassMatcher(JTabbedPane.class, true));
        } catch (ComponentNotFoundException | MultipleComponentsFoundException e) {
            if (e instanceof MultipleComponentsFoundException) {
                Component[] panes = ((MultipleComponentsFoundException) e).getComponents();
                for (Component component : panes) {
                    int tabCount = ((JTabbedPane) component).getTabCount();
                    for (int i = 0; i < tabCount; i++) {
                        String tabTitle = ((JTabbedPane) component).getTitleAt(i);
                        if (tabTitle != null && tabTitle.equals(tabName)) {
                            pane = (JTabbedPane) component;
                            break;
                        }
                    }
                    if (pane != null) {
                        break;
                    }
                }
            }
            else {
                throw e;
            }
        }
        JTabbedPaneTester jtester = new JTabbedPaneTester();
        jtester.actionSelectTab(pane, new JTabbedPaneLocation(tabName));
        abbot.tester.Robot.delay(3000);
    }

    public Component searchComponent(Component component, Matcher matcher) {
        try {
            component = new BasicFinder().find((Container) component, matcher);
        } catch (ComponentNotFoundException
                | MultipleComponentsFoundException e) {
            // TODO Auto-generated catch block
            Info.out(e.getMessage());
        }
        return component;
    }

    /**
     * 点击搜索框，搜索色带，并选择指定色带
     * @param colorComboBoxName 颜色方案对应的
     * @param colorSchemeName
     */
    public void clickColorScheme(String colorComboBoxName, String colorSchemeName) throws ComponentNotFoundException, MultipleComponentsFoundException {
        Component component = searchComponent(new NameMatcher(colorComboBoxName));
        clickComponent(component);
        robot.delay(2000);
        JPopupMenu jPopupMenu = ((ColorsComboBox) component).getPopupMenuColorScheme();
        Component[] components = jPopupMenu.getComponents();
        for (Component comp : components) {
            if(comp instanceof TextFieldSearch){
                JTextComponentTester jTextComponentTester = new JTextComponentTester();
                jTextComponentTester.actionEnterText(comp,colorSchemeName);
                robot.delay(2000);
                continue;
            }
            if (comp instanceof JScrollPane) {
                Component component1 = searchComponent(comp, new NameMatcher("ColorsComboBox_listColors"));
                if (component1 != null && component1 instanceof JList) {
                    JList list = ((JList) component1);
                    //list.getModel().getSize()获取的是当前显示的list的item个数
                    for (int i = 0; i < list.getModel().getSize(); i++) {
                        if (((ColorScheme) list.getModel().getElementAt(i)).getName().equals(colorSchemeName)) {
                            new ListOperation((JList) component1).selectIndex(i);
                            break;
                        }
                    }
                    robot.delay(3000);
                }
            }
        }
    }
}

